﻿//fastclick
Origami.fastclick(document.body);
$(function () {
    (function ($) {
        var swipelength = $('.db-banner-swipe').children('div').children('a').length;
        $('.db-banner-position span').html('/' + swipelength);
        if (swipelength > 1) {
            window.bannerSlider = Swipe(document.getElementById('swipe'), {
                auto: 3000,
                startSlide: 0,
                continuous: true,
                disableScroll: false,
                callback: function (index) {
                    if ((index + 1) > swipelength) {
                        index = index % 2;
                    }
                    $('.db-banner-position strong').html(index + 1)
                }
            });
            $('.db-banner-position').css("visibility", "visible")
        } else {
            $('.db-banner-swipe').css("visibility", "visible")
        }
    })(Zepto);


    //if (CFG.limitdaystart) {
    //    var start_time = "00:00";
    //    if (CFG.timedown) {
    //        start_time = CFG.startTime;
    //    }
    //    $('.dateRange').siblings().remove();
    //    $('.dateRange').html(CFG.startday + " " + start_time + " 开始");
    //    $('.timedown').show();
    //} else if (CFG.limitdayend) {
    //    $('.timedown').hide();
    //    $('.submit').prop('disabled', true).html('已结束');
    //}


    $('#db-content').css('padding-bottom', $('footer').height());

    $('.submit').on('click', function () {
        $.modal({
            type: 'confirm',
            title: '确定使用&nbsp;' + options.consumePoints + '&nbsp;分积兑换？',
            section: '',//'<p>兑换码将在兑换成功后发送给您，请按后续的提示使用。</p>',
            callback: {
                save: function () {
                    window.loading = $.modal({
                        type: 'loading',
                        title: '正在处理，请稍后···'
                    });

                    $.ajax({
                        url: options.exchangeItemUrl,
                        type: 'POST',
                        timeout: 5000,
                        dataType: 'json',
                        data: { itemId: options.itemId, sysItemId: 0, points: options.consumePoints },
                        success: function (response) {
                            if (response.status == 1) {

                                var url = options.orderDetailUrl + '/' + response.data;
                                location.href = url;

                            } else {
                                window.loading.close();
                                $.modal({
                                    type: 'alert',
                                    title: response.msg ? response.msg : '好可惜没抢到，再试一次吧',
                                    callback: {
                                        save: function () {
                                            if (response.status == -1) {
                                                window.location.href = options.custAddressUrl;

                                            } else {
                                                window.location.reload();
                                            }
                                        }
                                    }
                                });
                            }
                        },
                        error: function (e) {
                            ajaxError(e);
                        }
                    });
                }
            }
        })
    });

    if (navigator.userAgent.match(/(iphone|ipad|Android|ios)/ig)) {
        //app状态监控，用户10秒不触碰屏幕，即有可能离开了app，停止banner
        var inApp = true;
        function inAppTimerFn() {
            window.inAppTimer = setTimeout(function () {
                inApp = false;
                if (window.bannerSlider) {
                    bannerSlider.stop();
                }
            }, 12000);
        }
        inAppTimerFn();
        document.addEventListener('touchend', function (e) {
            if (window.inAppTimer) clearTimeout(window.inAppTimer);
            if (!inApp) {
                inApp = true;
                if (window.bannerSlider) {
                    bannerSlider.begin();
                }
            }
            inAppTimerFn();
        }, false)
    }
});
function ajaxError(e) {
    loading.close();

    if (e.status == 0) {
        $.modal({
            type: 'alert',
            title: '网络连接失败，请检查网络',
        });
    } else if (e.status == 404) {
        $.modal({
            type: 'alert',
            title: '找不到服务器地址，请重试',
        });
    } else {
        $.modal({
            type: 'alert',
            title: '系统发生错误，请重试',
        });
    }
}